﻿using System;
using B.S.DataBase.MyProject.Domain.Vendors;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.VendorsCommand;
using T.E.ERP.Write.API.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.VendorsCommandHandler
{
    public class GetMateriCommandHandler : IRequestHandler<GetMateriCommand, APIResult<ApiPaging<MateriDtos>>>
    {

        private readonly IBaseRepository<MaterialSupplier> matebaseRepository;

        private readonly IBaseRepository<Supplieditems> ditemsRepository;

        private readonly IBaseRepository<Supplierprovendor> provendorRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="matebaseRepository">原料供应商</param>
        /// <param name="ditemsRepository">供应料品表</param>
        /// <param name="provendorRepository">供应商分类</param>
        public GetMateriCommandHandler(IBaseRepository<MaterialSupplier> matebaseRepository, IBaseRepository<Supplieditems> ditemsRepository, IBaseRepository<Supplierprovendor> provendorRepository)
        {
            this.matebaseRepository = matebaseRepository;
            this.ditemsRepository = ditemsRepository;
            this.provendorRepository = provendorRepository;
        }

        public async Task<APIResult<ApiPaging<MateriDtos>>> Handle(GetMateriCommand request, CancellationToken cancellationToken)
        {
            APIResult<ApiPaging<MateriDtos>> result = new APIResult<ApiPaging<MateriDtos>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();

            var list = from a in await matebaseRepository.GetAll()
                       join b in await ditemsRepository.GetAll()
                       on a.Suppliedid equals b.Suppliedid
                       join c in await provendorRepository.GetAll()
                       on a.SupplierCid equals c.SupplierCid
                       select new MateriDtos
                       {
                           Supid =a.Supid,
                           SupplierName = a.SupplierName,
                           Supption = a.Supption,
                           Province = a.Province,
                           City = a.City,
                           County = a.County,
                           Suppliedid = a.Suppliedid,
                           SupplierCid = a.SupplierCid,
                           SupplierG = a.SupplierG,
                           SuppAddress = a.SuppAddress,
                           PostalCode = a.PostalCode,
                           Telephone = a.Telephone,
                           Status = a.Status,
                           SBank = a.SBank,
                           SAddress = a.SAddress,
                           TaxNumber = a.TaxNumber,
                           Bines = a.Bines,
                           SAddAccount = a.SAddAccount,
                           BinesPhone = a.BinesPhone,
                           Financial = a.Financial,
                           FinancialPhone = a.FinancialPhone,
                           SupplierCName = c.SupplierCName,
                           SuppliedName = b.SuppliedName,
                       };

            //供应商名称
            if (!string.IsNullOrEmpty(request.SupplierName))
            {
                list = list.Where(x=>x.SuppliedName.Contains(request.SupplierName));
            }

            //财务联系人
            if (!string.IsNullOrEmpty(request.Financial))
            {
                list = list.Where(x => x.SuppliedName.Contains(request.Financial));
            }
            //供应料品(外键)
            if (request.Suppliedid!=0)
            {
                list =list.Where(x=>x.Suppliedid==request.Suppliedid);
            }
            int TotalCount = list.Count();
            int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.Supid).Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize);


            ApiPaging<MateriDtos> api = new ApiPaging<MateriDtos>();
            api.TotalCount = TotalCount;
            api.TotalPage = TotalPage;
            api.list = list.ToList();
            result.data = api;
            //result.data =list.ToList();
            return await Task.FromResult(result);
        }
    }
}
